package com.ebelter.nb.model.jg;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;

import com.ebelter.btcomlib.models.bluetooth.products.nb.BondDevices;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.nb.constants.NbConstants;
import com.ebelter.nb.ui.view.ScaleDataTipDialog;
import com.ebelter.nb.utils.ScaleUtils;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.BaseHandle;
import com.ebelter.btcomlib.utils.GsonUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.nb.model.jg.jgbean.JGBaseResponse;
import com.ebelter.nb.model.jg.jgbean.JGBpmR;
import com.ebelter.nb.model.jg.jgbean.JGScaleR;
import com.ebelter.nb.model.nb.uploaders.DataUploader;
import com.ebelter.nb.ui.activitys.MainActivity;
import com.ebelter.nb.utils.NbUtitls;
import org.greenrobot.eventbus.EventBus;
import cn.jpush.android.api.JPushInterface;

//极光信息处理器
public class JGMsgProcessor extends BaseHandle{
    public static final String TAG = "JGMsgProcessor";
    private GsonUtils mGsonUtils;
    private DataUploader mDataUploader;
    private ScaleUtils mScaleUtils;
    private static JGMsgProcessor instance;
    public static JGMsgProcessor getInstance() {
        if (instance==null){
            synchronized (JGMsgProcessor.class){
                instance = new JGMsgProcessor();
            }
        }
        return instance;
    }

    private JGMsgProcessor() {
        this.mGsonUtils = GsonUtils.getInstance();
        mDataUploader = new DataUploader();
        mScaleUtils = new ScaleUtils();
    }
    //做成单例模式
    public static final int CMD_BPM = 1;
    public static final int CMD_SCALE = 2;
    public static final int CMD_SCALE_SENDINFO2SERVICES = 3;

    void receiveMsg(Context context, Bundle bundle) {
        if (context==null||bundle==null)return;

        String extraMsg = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        String extraExtra = bundle.getString(JPushInterface.EXTRA_EXTRA);
        LogUtils.i(TAG, "[MyReceiver] EXTRA_MESSAGE : " + extraMsg);
        LogUtils.i(TAG, "[MyReceiver] EXTRA_EXTRA: " + extraExtra);

        //{"deviceType":1,"deviceInfo":{"rssi":10,"unit":0,"usermode":2,"fat":"193","weight":"678","imei":"867726036757393","resistor":"493","hart":"90","time":"2019-06-12 09:23:19"}}


      //  ToastUtil.show(extraMsg+"###"+extraExtra);

        if (!TextUtils.isEmpty(extraMsg)){
            JGBaseResponse jgB = mGsonUtils.fromJson(extraMsg,JGBaseResponse.class);
            if (jgB!=null){
                if (0==jgB.deviceType){//血压计
                    dealBpm(extraMsg);
                }else if (1==jgB.deviceType){//脂肪秤
                    dealScale(context,extraMsg);
                }
            }
        }

    }
    //处理体脂秤秤的
    public void dealScale(Context context,String extraMsg) {
        LogUtils.i(TAG,"极光收到-体脂秤----extraMsg = "+extraMsg);
        JGScaleR jGScaleR = mGsonUtils.fromJson(extraMsg,JGScaleR.class);
        if (jGScaleR==null||jGScaleR.deviceInfo==null)return;

        BondDevices bondDevices = NbUtitls.getBondScaleDevice();
        if (bondDevices==null)return;

        if (!TextUtils.equals(jGScaleR.deviceInfo.imei,bondDevices.device_id)){
            LogUtils.i(TAG,"体脂秤-接收到的imei值跟绑定的设备的imei值不一致 reture bondDevices="+bondDevices.toString());
            return;
        }

        jGScaleR = mScaleUtils.dealJGScaleR(jGScaleR);

        LogUtils.i(TAG,"极光收到-体脂秤----jGScaleR = "+jGScaleR);
        if (jGScaleR.deviceInfo.usermode == 1){//用户模式 00 = 未识别到用户01=用户模式下测量数据(单用户)、02=用户模式下测量数据(多用户)、03=工厂模式测试数据
            surejGScaleRData(jGScaleR);
        }else {
            showScaleTipDialog(context,jGScaleR);
        }
    }

    private void showScaleTipDialog(Context context, final JGScaleR jGScaleR) {

        LogUtils.i(TAG,"极光收到-体脂秤----showScaleTipDialog ");
        Activity activity = ActivityManagers.getInstance().getTopActivity();
        if (activity!=null){
            LogUtils.i(TAG,"极光收到-体脂秤----showScaleTipDialog activity != null");
            ScaleDataTipDialog scaleDataTipDialog=new ScaleDataTipDialog(activity);
            scaleDataTipDialog.setListener(new ScaleDataTipDialog.ICancelOkBtListener() {
                @Override
                public void cancel(Dialog dialogView) {
                    LogUtils.i(TAG,"---cancle");
                    dialogView.cancel();
                }
                @Override
                public void ok(Dialog dialogView) {
                    LogUtils.i(TAG,"---ok");
                    dialogView.cancel();
                    surejGScaleRData(jGScaleR);

                    CommonEventBus<JGScaleR> jgBpmEB = new CommonEventBus<>(TAG, MainActivity.TAG,CMD_SCALE_SENDINFO2SERVICES,"发送用户信息到服务器");
                    EventBus.getDefault().post(jgBpmEB);
                }
            });
            scaleDataTipDialog.show();

            String tizhiTip = jGScaleR.deviceInfo.fat2>0?jGScaleR.deviceInfo.fat2+"%":"--";

            String tip = "体重 : "+ NumUtils.numBaoLiuWei1(jGScaleR.deviceInfo.weight2)+"kg \n" +
                    "体脂 : "+tizhiTip+"\n"+
                    "时间 : "+jGScaleR.deviceInfo.time;
            scaleDataTipDialog.setTip(tip);
        }



    }


    private void surejGScaleRData(JGScaleR jGScaleR){

        UserSpUtil.writeFloat(NbConstants.User.weight,NumUtils.numBaoLiuWei1(jGScaleR.deviceInfo.weight2));//保存最新的体重到本地
        //第一步显示在界面上
        CommonEventBus<JGScaleR> jgBpmEB = new CommonEventBus<>(TAG, MainActivity.TAG,CMD_SCALE,null);
        jgBpmEB.setContent(jGScaleR);
        EventBus.getDefault().post(jgBpmEB);

        //第二步上传至相对应的网络数据库中
        if (jGScaleR.deviceInfo!=null&&mDataUploader!=null){
            jGScaleR.deviceInfo.mesureDataTyle = 1;//1代表体脂秤的标识
            jGScaleR.deviceInfo.productTime = jGScaleR.deviceInfo.time;
            jGScaleR.deviceInfo.authKey = NbUtitls.getLoginAuthkey();
            jGScaleR.deviceInfo.uuid = NbUtitls.getLoginUserId();
            if (!TextUtils.isEmpty(jGScaleR.deviceInfo.authKey)&&!TextUtils.isEmpty(jGScaleR.deviceInfo.uuid)){
                mDataUploader.starUploadThread();//让上传线程运行起来
                mDataUploader.addData(jGScaleR.deviceInfo);
            }
        }
    }

    //处理血压计的
    public void dealBpm(String extraMsg) {
        JGBpmR jgBpmR = mGsonUtils.fromJson(extraMsg,JGBpmR.class);
        if (jgBpmR==null||jgBpmR.deviceInfo==null)return;

        BondDevices bondDevices = NbUtitls.getBondBpmDevice();
        if (bondDevices==null)return;

        if (!TextUtils.equals(jgBpmR.deviceInfo.imei,bondDevices.device_id)){
            LogUtils.i(TAG,"血压计-接收到的imei值跟绑定的设备的imei值不一致 reture bondDevices = "+bondDevices.toString());
            return;
        }

        //第一步显示在界面上
        CommonEventBus<JGBpmR> jgBpmEB = new CommonEventBus<>(TAG, MainActivity.TAG,CMD_BPM,null);
        jgBpmEB.setContent(jgBpmR);
        EventBus.getDefault().post(jgBpmEB);

        //第二步上传至相对应的网络数据库中
        if (jgBpmR.deviceInfo != null && mDataUploader != null){
            jgBpmR.deviceInfo.mesureDataTyle = 0;//0代表血压计的标识
            jgBpmR.deviceInfo.productTime = jgBpmR.deviceInfo.time;
            jgBpmR.deviceInfo.authKey = NbUtitls.getLoginAuthkey();
            jgBpmR.deviceInfo.uuid = NbUtitls.getLoginUserId();
            jgBpmR.deviceInfo.userType = jgBpmR.deviceInfo.user;
            jgBpmR.deviceInfo.deviceId = jgBpmR.deviceInfo.imei;

            if (!TextUtils.isEmpty(jgBpmR.deviceInfo.authKey)&&!TextUtils.isEmpty(jgBpmR.deviceInfo.uuid)){
                mDataUploader.starUploadThread();//让上传线程运行起来
                mDataUploader.addData(jgBpmR.deviceInfo);
            }
        }

    }


}
